package com.zr.corporatefactory.service.impl;

import com.zr.corporatefactory.mapper.CorFacMapper;
import com.zr.corporatefactory.model.*;
import com.zr.corporatefactory.service.CorFactoryService;
import com.zr.util.AllRecords;
import com.zr.util.ErrorMessageClass;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by 天赐 on 2019/12/24.
 */
@Service
public class CorFactoryServiceImpl implements CorFactoryService{

    @Autowired
    private CorFacMapper corFacMapper;
    @Override
    public ResultVo getPage(PageSelectVo pageSelectVo) {
        List<Factory> facList = corFacMapper.queryPage(pageSelectVo);

        Integer count = corFacMapper.queryCount(pageSelectVo);



        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(facList);
        allRecords.setTotalNumber(count);
        allRecords.setPageIndex(pageSelectVo.getPageIndex());
        allRecords.setPageSize(pageSelectVo.getPageSize());

        allRecords.resetTotalNumber(count);
        return ResultVoBuilder.success(allRecords);

    }



    @Override
    public ResultVo update(@Valid CorUpdate corUpdate) {

        Factory factory1 = corFacMapper.queryById(corUpdate.getLegalPlantCode());
        int nameCount =  corFacMapper.queryByName(corUpdate.getLegalPlantDec());
        if (nameCount>0){

            if (corUpdate.getLegalPlantDec().equals(factory1.getLegalPlantDec())){
                corFacMapper.update(corUpdate);
                return ResultVoBuilder.success();
            }
            return ResultVoBuilder.error(ErrorMessageClass.PLANCLASSADD);
        }


        corFacMapper.update(corUpdate);
        return ResultVoBuilder.success();
    }

    @Override
    public ResultVo updateStatus(CorFacStatus corFacStatus) {
        corFacMapper.updateStatus(corFacStatus);

        return ResultVoBuilder.success("修改成功");
    }

    @Override
    public List<legalperson> getPersonName() {
        List<legalperson> list = corFacMapper.getPersonName();
        return list;
    }

    @Override
    public ResultVo addCor(@Valid CorAdd corAdd) {

        /*根据法人编码,查询法人名称,并传递到后台*/
        Corporate corporate = corFacMapper.getPersonCode(corAdd.getLegalPersonCode());
        System.out.println(corporate);
        corAdd.setLegalPersonName(corporate.getLegalPersonName());

        System.out.println(corAdd);


        corFacMapper.addCor(corAdd);
        return ResultVoBuilder.success();
    }
}
